using System.Collections.Concurrent;
using JMCOM.Communication;

namespace JMCOM;

public class JmcomManager
{ 
    #region TcpClientManager

    interface ITcpClientManager
    {
        bool Add(string key, TCPIPClient client);
        bool Remove(string key);
        bool TryGet(string key, out TCPIPClient client);
        IEnumerable<TCPIPClient> GetAll();
    }

    public class TcpClientManager : ITcpClientManager
    {
        // key 可以是设备 ID、IP:Port 或 Guid
        private readonly ConcurrentDictionary<string, TCPIPClient> _clients = new();

        public bool Add(string key, TCPIPClient client)
            => _clients.TryAdd(key, client);

        public bool Remove(string key)
            => _clients.TryRemove(key, out _);

        public bool TryGet(string key, out TCPIPClient client)
            => _clients.TryGetValue(key, out client);

        public IEnumerable<TCPIPClient> GetAll()
            => _clients.Values;
    }

    #endregion
}